﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using HYRevitCode.CurveUtility;

namespace HYRevitCode.RevitTopologySuite
{
	internal class ArcBackBean
	{
		internal ArcBackBean(Arc inputArc)
		{
			this.m_thisArc = inputArc;
		}

		internal bool IfPointOnThisArc(XYZ input)
		{
			return CurveUtilityMethod.IfPointOnCurve(this.m_thisArc, input, true) && !this.IfPointInList(input);
		}

		internal void AppendPoint(XYZ inputPoint)
		{
			if (inputPoint != null)
			{
				if (this.m_AllPoints == null)
				{
					this.m_AllPoints = new List<XYZ>();
				}
				this.m_AllPoints.Add(inputPoint);
			}
		}

		internal Curve GetBackArc()
		{
			if (this.m_AllPoints.Count < 1)
			{
				throw new ArgumentException();
			}
			Curve result;
			try
			{
				if (this.m_AllPoints.Count == 2)
				{
					result = Line.CreateBound(this.m_AllPoints[0], this.m_AllPoints[1]);
				}
				else
				{
					result = Arc.Create(this.m_AllPoints[0], this.m_AllPoints[this.m_AllPoints.Count - 1], this.m_AllPoints[this.m_AllPoints.Count / 2]);
				}
			}
			catch (Exception ex)
			{
				throw new ArgumentException(ex.Message);
			}
			return result;
		}

		private bool IfPointInList(XYZ input)
		{
			bool result = false;
			using (List<XYZ>.Enumerator enumerator = this.m_AllPoints.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.IsAlmostEqualTo(input))
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		private Arc m_thisArc;

		private List<XYZ> m_AllPoints;
	}
}
